import { hrmRetiredApi } from "@/api/HRMS/Retired";
import { useDrawer } from "@/components/Drawer";
import { BasicColumn, BasicTable, FormSchema, TableAction, useTable } from "@/components/Table";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { message, Tag } from "antd";
import CreateAndUpdata from "./components/createAndUpdata";
import { useTranslation } from "react-i18next";
import { IconEnum } from "@/enums/appEnum";

const index = () => {
	const { t } = useTranslation();
	const ev_vi = "HRMS.retired";
	const schemas: FormSchema[] = [
		{
			label: t(`${ev_vi}.staffName`),
			field: "staffName", //姓名
			component: "Input"
		},
		{
			label: t(`${ev_vi}.status`),
			field: "approveStatus", //状态
			component: "Select",
			componentProps: {
				options: [
					{ label: t(`${ev_vi}.retired`), value: "1" },
					{ label: t(`${ev_vi}.notRetired`), value: "0" }
				]
			}
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${ev_vi}.staffName`),
			dataIndex: "staffName"
		},
		{
			title: t(`${ev_vi}.timeToWork`),
			dataIndex: "startDate"
		},
		{
			title: t(`${ev_vi}.retirementTime`),
			dataIndex: "endDate"
		},
		{
			title: t(`${ev_vi}.status`),
			dataIndex: "approveStatus",
			render: (status: any) => {
				if (status == 1) return <Tag color="success">{t(`${ev_vi}.success`)}</Tag>;
				if (status == 0) return <Tag color="processing">{t(`${ev_vi}.inApproval`)}</Tag>;
				return <Tag color="error">{t(`${ev_vi}.fail`)}</Tag>;
			}
		},
		{
			title: t("kevin_ui.operate"),
			render: (_, record) => {
				return (
					<TableAction
						actions={[
							{
								icon: IconEnum.EDIT,
								label: t("felix_ui.edit"),
								onClick: upDate.bind(record)
							},
							{
								icon: IconEnum.DELETE,
								label: t("felix_ui.delete"),
								popConfirm: {
									title: t("common.deleteMessage"),
									placement: "left",
									confirm: deleteFn.bind(null, record)
								}
							}
						]}
					></TableAction>
				);
			}
		}
	];
	const [register, { reload }] = useTable({
		api: hrmRetiredApi.hrmRetiredPage,
		columns,
		useSearchForm: true,
		formConfig: {
			labelWidth: 100,
			schemas,
			showActionButtonGroup: true,
			baseColProps: { span: 6 },
			actionColOptions: { span: 24 }
		},
		bordered: false
	});
	const [registerDrawer, { openDrawer }] = useDrawer();
	const handlerToolbarAction = (key: string) => {
		switch (key) {
			case "add":
				openDrawer(true, {
					reload
				});
				break;
		}
	};
	const upDate = (record: any) => () => {
		openDrawer(true, {
			reload,
			record
		});
	};
	const deleteFn = async (record: any) => {
		console.log(record);

		const res = await hrmRetiredApi.hrmRetiredDelete({ id: record.pkId });
		console.log(res);
		if (!res) return message.error(t("kevin_ui.deleteFailed"));
		message.success(t("kevin_ui.daleteSucceessfully"));
		reload();
	};
	return (
		<div className="page">
			<div className="page-content">
				<BasicTable register={register}></BasicTable>
			</div>
			<CreateAndUpdata register={registerDrawer}></CreateAndUpdata>
			<Toolbar action={handlerToolbarAction} btnList={["add"]} />
		</div>
	);
};
export default index;
